package com.example;

import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletContextListener;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/a")
public class MyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        ServletContext context = req.getServletContext();
        System.out.println("向ServletContext对象中存数据");
        context.setAttribute("uname", "zhangsan");

        System.out.println("修改ServletContext对象中的数据");
        context.setAttribute("uname", "lisi");

        System.out.println("从ServletContext对象中移除数据");
        context.removeAttribute("uname");
    }
}